package cn.kgc.service.impl;

import cn.kgc.base.dto.LockStockDTO;
import cn.kgc.commons.exception.HttpException;
import cn.kgc.entity.WmsWareInfo;
import cn.kgc.mapper.WmsWareInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.mapper.WmsWareSkuMapper;
import cn.kgc.entity.WmsWareSku;
import cn.kgc.service.WmsWareSkuService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品库存(WmsWareSku)表服务实现类
 *
 * @author makejava
 * @since 2022-12-07 10:41:17
 */
@Service("wmsWareSkuService")
public class WmsWareSkuServiceImpl extends ServiceImpl<WmsWareSkuMapper, WmsWareSku> implements WmsWareSkuService {


    @Resource
    private WmsWareInfoMapper wmsWareInfoMapper;

    @Resource
    private WmsWareSkuMapper wmsWareSkuMapper;

    @Resource
    private RabbitTemplate rabbitTemplate;



    @Transactional
    public boolean lockStock(List<LockStockDTO> lockStockDTOList) {
        //遍历需要所的商品信息
        for (LockStockDTO lockStockDTO : lockStockDTOList) {
            String areaCode=lockStockDTO.getAreaCode();
            LambdaQueryWrapper<WmsWareInfo>lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WmsWareInfo::getAreacode,areaCode);

            WmsWareInfo wmsWareInfo = wmsWareInfoMapper.selectOne(lambdaQueryWrapper);
            Long wareId = wmsWareInfo.getId();

            //判断库存是否充足 如果库存不充足 锁定商品失败
            LambdaQueryWrapper<WmsWareSku>lambdaQueryWrapper2=new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(WmsWareSku::getSkuId,lockStockDTO.getSkuId());
            lambdaQueryWrapper2.eq(WmsWareSku::getWareId,wareId);
            WmsWareSku wmsWareSku = wmsWareSkuMapper.selectOne(lambdaQueryWrapper2);
            int nowStock=wmsWareSku.getStock()-wmsWareSku.getStockLocked();
            //库存不足
            //库存不足
            if (nowStock < lockStockDTO.getLockStock()) {
                throw  new HttpException(40008, HttpStatus.INSUFFICIENT_STORAGE.value());
            }else {
                //库存充足  原有锁定的库存+现在锁定的库存
                //锁定库存
                wmsWareSku.setStockLocked(wmsWareSku.getStockLocked()+lockStockDTO.getLockStock());
               //更新操作有问题，返回的结果应该是0（version）
                int result = wmsWareSkuMapper.updateById(wmsWareSku);
                if (result==0){
                    throw  new HttpException(40008,
                            HttpStatus.INSUFFICIENT_STORAGE.value());
                }
            }
        }
        rabbitTemplate.convertAndSend("stock.exchange",
                "lock.stock",lockStockDTOList);

        //将锁定库存成功的信息发送到消息队列中
        return true;
    }

    @Override
    public boolean unlockStock(List<LockStockDTO> lockStockDTOList) {
        for (LockStockDTO lockStockDTO : lockStockDTOList) {
            String areaCode = lockStockDTO.getAreaCode();//获取地区编码
            LambdaQueryWrapper<WmsWareInfo> lambdaQueryWrapper =
                    new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WmsWareInfo::getAreacode, areaCode);
            WmsWareInfo wmsWareInfo = wmsWareInfoMapper.selectOne(lambdaQueryWrapper);
            Long wareId = wmsWareInfo.getId(); //获得仓库的ID

            LambdaQueryWrapper<WmsWareSku>lambdaQueryWrapper2=new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(WmsWareSku::getSkuId,lockStockDTO.getSkuId());
            lambdaQueryWrapper2.eq(WmsWareSku::getWareId,wareId);
            WmsWareSku wmsWareSku = wmsWareSkuMapper.selectOne(lambdaQueryWrapper2);
            int nowStock=wmsWareSku.getStockLocked()-lockStockDTO.getLockStock();
            wmsWareSku.setStockLocked(nowStock);
            int result = wmsWareSkuMapper.updateById(wmsWareSku);
            if (result==0){
                throw  new HttpException(40009,
                        HttpStatus.INSUFFICIENT_STORAGE.value());
            }
        }
        return true;
    }
}
